function $(ElementID) {
	return document.getElementById(ElementID);
}

window.onload = init;

function init() {
	$("username").onfocus = clear;
	$("password").onfocus = clear;
	$("captcha").onfocus = clear;
}

function validForm() {
	var loginName = $("username").value;
	var pwd = $("password").value;
	var captcha = $("captcha").value;
	var prompt = $("prompt");
	if(loginName == ""){
		if(pwd == ""){
			if(captcha == ""){
				prompt.innerHTML = "请输入账号、密码和验证码";
				prompt.style.display = "block";
				return false;
			}
			prompt.innerHTML = "请输入账号和密码";
			prompt.style.display = "block";
			return false;
		}
		if(captcha == ""){
			prompt.innerHTML = "请输入账号和验证码";
			prompt.style.display = "block";
			return false;
		}
		prompt.innerHTML = "请输入账号";
		prompt.style.display = "block";
		return false;
	} else if(pwd == ""){
		if(captcha == ""){
			prompt.innerHTML = "请输入密码和验证码";
			prompt.style.display = "block";
			return false;
		}
		prompt.innerHTML = "请输入密码";
		prompt.style.display = "block";
		return false;
	} else if(captcha == ""){
		prompt.innerHTML = "请输入验证码";
		prompt.style.display = "block";
		return false;
	} else if(checkLoginName() == false){
		prompt.innerHTML = "账号请输入6位的英文字母和数字";
		prompt.style.display = "block";
		return false;
	} else if(checkPwd() == false){
		prompt.innerHTML = "密码请输入6位的英文字母和数字";
		prompt.style.display = "block";
		return false;
	} else if(checkCaptcha() == false){
		prompt.innerHTML = "验证码请输入4位的英文字母";
		prompt.style.display = "block";
		return false;
	} else if(checkDtef() == false){
		prompt.innerHTML = "验证码输入错误,请重新输入";
		prompt.style.display = "block";
		return false;
	} else{
		var usernameValue = document.getElementById("username").value;
		var passwordValue = document.getElementById("password").value;
		var flag = false; // 校验的标志
		for(var i = 0;i < localStorage.length;i++){
			var keyName = localStorage.key(i);
			// 用户名找到，取出value
			if(keyName == usernameValue){
				var str = localStorage.getItem(keyName);
				// 将str转为对象
				var user = JSON.parse(str);
				// 用户密码的判断
				if(user.password == passwordValue){
					// 登录校验成功后，往sessionStorage中放入用户名信息
					sessionStorage.setItem("loginUser",usernameValue);
					// 登录校验成功后，往localStorage中放入goods.id信息
					if(localStorage.getItem("id") == null){
						localStorage.setItem("id",0);
					}
					flag = true;
				}
			}
		}
		if(!flag){
			alert("用户名或密码有误");
		}
		return flag;
	}
}

// 验证登录账号
function checkLoginName() {
	var loginName = $("username").value;
	var regLoginName = /^[a-zA-Z0-9]{6}$/;
	if (regLoginName.test(loginName) == false) {
		return false;
	}
	return true;
}

// 验证登录密码
function checkPwd(){
	var pwd = $("password").value;
	var regPwd = /^[a-zA-Z0-9]{6}$/;
	if(regPwd.test(pwd) == false) {
		return false;
	}
	return true;
}

// 验证验证码格式是否正确
function checkCaptcha(){
	var captcha = $("captcha").value;
	var regCaptcha = /^[a-zA-Z]{4}$/;
	if(regCaptcha.test(captcha) == false) {
		return false;
	}
	return true;
}

// 验证验证码内容是否正确
function checkDtef(){
	var captcha = $("captcha").value;
	var dtef = /^[Dd]{1}[Tt]{1}[Ee]{1}[Ff]{1}$/;
	if(dtef.test(captcha) == false){
		return false;
	}
	return true;
}

// 清除提示信息
function clear(){
	$("prompt").style.display = "none";
}